package com.borqs.music.tasks;

import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.http.RequestMethod;
import com.borqs.music.model.SongInfo;
import com.borqs.music.util.MD5Utils;
import com.borqs.music.util.MusicConf;
import com.borqs.music.util.MyLogger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONStringer;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

public class QueryUploadLyricTask extends Task {
    private static final MyLogger logger = MyLogger.getLogger(QueryUploadLyricTask.class.getSimpleName());

    private static final String TITLE = "title";
    private static final String SINGER = "singer";
    private static final String DURATION = "duration";
    private static final String CONTENT = "content";
    private static final String ISLOCAL = "islocal";
    private static final String SONGMD5 = "songmd5";
    private static final String LYRICMD5 = "lyricmd5";

    private String mTicket;
    private String mLyricJSON;
    private SongInfo mSong;
    private boolean isNeedUpload = false;

    public QueryUploadLyricTask(TaskListener listener, String ticket,
            SongInfo song) throws JSONException {
        super(listener);
        this.mTicket = ticket;
        this.mSong = song;
        this.mLyricJSON = encodeJSON(song, MD5Utils.getMD5(song.lyrics.getBytes()));
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        String songJSON = resp.asString();
        JSONArray jArray = new JSONArray(songJSON);
        if (TextUtils.isEmpty(songJSON) || jArray.length() < 1) {
            logger.d("Music server exist this lyric, no need upload.");
            isNeedUpload = false;
        } else {
            isNeedUpload = true;
        }

        return mSong;
    }

    @Override
    public HttpRequest prepareRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("lyriclist", mLyricJSON);

        String URL = MusicConf.getUrl(MusicConf.ReqType.MUSIC_LYRIC_SYNC);
        HttpRequest request = new HttpRequest(RequestMethod.POST, URL, mTicket, params);
        return request;
    }

    public boolean isNeedtoUpload() {
        return isNeedUpload;
    }

    private String encodeJSON(SongInfo song, String lyricMD5) throws JSONException {
        JSONStringer jsStringer = new JSONStringer();
        jsStringer.array();
        jsStringer.object();
        jsStringer.key(TITLE).value(song.title);
        jsStringer.key(SINGER).value(song.artist);
        jsStringer.key(DURATION).value(song.duration);
        jsStringer.key(CONTENT).value(song.lyrics);
        jsStringer.key(ISLOCAL).value(song.isLocalLyric);
        jsStringer.key(SONGMD5).value(song.fileMd5);
        jsStringer.key(LYRICMD5).value(lyricMD5);
        jsStringer.endObject();
        jsStringer.endArray();

        return jsStringer.toString();
    }

}
